package mail.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import mail.AccountManager;
import mail.MailSession;
import mail.MultiEmail;
import mail.bean.AccountBean;
import mail.bean.EmailBean;

public class AccountPanel extends JPanel {
  public AccountPanel(MultiEmail multiEmail) {
    super();
    setPreferredSize(new Dimension(200, 500));
    this.setName("AccountPanel");
    this.top = multiEmail;
     
    accountList = AccountManager.loadAccount();
    for(AccountBean acct: accountList) {
      sessionMap.put(acct.getAccountName(), new MailSession(acct, top));
    }
    
    acctPopup = new JPopupMenu();
    JMenuItem recMenu = new JMenuItem("Receive");
    recMenu.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)accountTree.getLastSelectedPathComponent();
        String acctName = (String) node.getUserObject();
        MailSession session = sessionMap.get(acctName);
        session.receiveMailBackgroup();
      }
    });
    acctPopup.add(recMenu);
    
    accountTree = new JTree(constructAccountTree(accountList));
    accountTree.setPreferredSize(new Dimension(200, 500));
    accountTree.setRootVisible(false);
    accountTree.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)accountTree.getLastSelectedPathComponent();
        if(node == null) {
          return;
        }
        if(MouseEvent.BUTTON3 == e.getButton()) {          
          if(node.getLevel() == 1) {//account level
            AccountBean acct = accountList.get(node.getParent().getIndex(node));
            acctPopup.show(accountTree, e.getX(), e.getY());
          }
        }else if(MouseEvent.BUTTON1 == e.getButton()) {
          if(node.getLevel() == 1) {//account level
            acct = accountList.get(node.getParent().getIndex(node));
            if(acct.getMailList() == null) {
              String inboxPath = acct.getSaveFolder() + "/" + AccountBean.INBOX;
              acct.setMailList(AccountManager.loadMailList(inboxPath));
              acct.setUidList(AccountManager.getUidList(acct.getMailList()));
            }
            top.getListPanel().getModel().setEmailList(acct.getMailList());
            top.getListPanel().getTable().updateUI();
          }
        }
        
      }
    });
    
    final GroupLayout groupLayout = new GroupLayout((JComponent) this);
    groupLayout.setHorizontalGroup(
    	groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
	    	.addComponent(accountTree, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
    );
    groupLayout.setVerticalGroup(
    	groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
	    	.addComponent(accountTree, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
    );
    setLayout(groupLayout);
    
    
  }
  
  private JTree accountTree;
  private JPopupMenu acctPopup;
  private ArrayList<AccountBean> accountList = null;
  private AccountBean acct = null;
  private MultiEmail top = null;
  private HashMap<String, MailSession> sessionMap = new HashMap<String, MailSession>();
  
  private TreeNode constructAccountTree(ArrayList<AccountBean> list) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Email Account");
    if(list != null && list.size() > 0) {
      for(AccountBean account : list) {
        root.add(account.toTree());
      }
    }
    return root;
  }

  public AccountBean getAcct() {
    return acct;
  }

  public void setAcct(AccountBean acct) {
    this.acct = acct;
  }

  public ArrayList<AccountBean> getAccountList() {
    return accountList;
  }

  public void setAccountList(ArrayList<AccountBean> accountList) {
    this.accountList = accountList;
  }
}
